package com.huonan.service.orderservice.service;

import com.huonan.service.orderservice.domain.Item;
import com.huonan.service.orderservice.domain.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author huonan
 * @date 2020/2/27 16:31
 * @desc
 */
@Service
public class OrderService {

    @Autowired
    private ItemService apiItemService;


    private static final Map<String, Order> MAP = new HashMap<>();

    static {

        Item item1 = new Item();
        Item item2 = new Item();
        item1.setId(1);
        item2.setId(2);
        List<Item> itemList = new ArrayList<>();
        itemList.add(item1);
        itemList.add(item2);

        Order order = new Order();
        order.setId("123456789");
        order.setCreateDate(new Date());
        order.setItems(itemList);

        MAP.put(order.getId(), order);
    }

    public Order queryOrderById(String id) {
        Order order = MAP.get(id);
        List<Item> items = new ArrayList<>();
        for (Item item : order.getItems()) {
            //调用订单微服务，重新设置商品数据
            Item temp = apiItemService.queryItemById(item.getId());
            items.add(temp);
        }
        order.setItems(items);
        return order;
    }

    private static final Map<Integer, Item> map = new HashMap<>();
}
